package com.mbc.rmt.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.mbc.common.dao.DAO;
import com.mbc.rmt.entities.CashPaymentBySentDetail;

public class CashPaymentBySentDetailDAOImpls extends DAO<CashPaymentBySentDetail> implements CashPaymentBySentDetailDAO{

	public CashPaymentBySentDetailDAOImpls(Connection conn)
	{
		this.conn = conn;
		this.table_name = "CashPaymentBySentDetail";
	}

	@Override
	public void create(CashPaymentBySentDetail entity) throws SQLException {
		query = "insert into " + table_name + " ( CashPaymentBySentDetailID, SlipID, InvoiceNo, ReceiptNo, Sender, Receiver, Currency, Amount, Paided, ReturnStatus, Apply, Balance, TotalAmount) values( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		pstmt = conn.prepareStatement(query);
		int i = 1;
				pstmt.setLong(i++, entity.getCashPaymentBySentDetail_id());
		pstmt.setLong(i++, entity.getSlip_id());
		pstmt.setString(i++, entity.getInvoiceno());
		pstmt.setString(i++, entity.getReceiptno());
		pstmt.setString(i++, entity.getSender());
		pstmt.setString(i++, entity.getReceiver());
		pstmt.setString(i++, entity.getCurrency());
		pstmt.setDouble(i++, entity.getAmount());
		pstmt.setString(i++, entity.getPaided());
		pstmt.setString(i++, entity.getReturn_code());
		pstmt.setString(i++, entity.getApply());
		pstmt.setDouble(i++, entity.getBalance());
		pstmt.setDouble(i++, entity.getTotalamount());

		pstmt.executeUpdate();
		pstmt.close();
		
	}

	@Override
	public void deleteByCashPaymentBySentID(long cashPaymentBySentID)
			throws SQLException {
		query = getDeleteQuery() + " where SlipID = " + cashPaymentBySentID;
		pstmt = conn.prepareStatement(query);
		pstmt.executeUpdate();
		pstmt.close();
		
	}

	@Override
	public List<CashPaymentBySentDetail> findByCashPaymentBySentID(
			long cashPaymentBySentID) throws SQLException {
		ArrayList<CashPaymentBySentDetail> l_list = new ArrayList<CashPaymentBySentDetail>();
		query = "select ROW_NUMBER()over(order by bcd.branchchangedetailID)No, "+
				"u.LoginId, "+
				"u.UserName, "+
				"b.BranchCode CurBranchID, "+
				"b.BranchCode ToBranchID, "+
				"bcd.remark "+
				"from BranchChangeDetail bcd "+
				"inner join BranchChange bc on bc.branchchangeID = bcd.branchchangeID "+
				"inner join Branch b on b.BranchId = bcd.branchID "+
				"inner join [User] u on u.userID = bcd.userID" +
				"where bcd.branchchangeID = "+cashPaymentBySentID;
		pstmt = conn.prepareStatement(query);
		rs = pstmt.executeQuery();
		while (rs.next()) {
			CashPaymentBySentDetail l_detail = readRecord(rs);
			l_detail.setInvoiceno(rs.getString("InvoiceNo"));
			l_list.add(l_detail);
		}
		rs.close();
		pstmt.close();
		return l_list;
	}

	@Override
	protected CashPaymentBySentDetail readRecord(ResultSet rs)
			throws SQLException {
		CashPaymentBySentDetail l_entity = new CashPaymentBySentDetail();
		l_entity.setCashPaymentBySentDetail_id(rs.getLong("CashPaymentBySentDetailID"));
		l_entity.setSlip_id(rs.getLong("SlipID"));
		l_entity.setInvoiceno(rs.getString("InvoiceNo"));
		l_entity.setReceiptno(rs.getString("ReceiptNo"));
		l_entity.setSender(rs.getString("Sender"));
		l_entity.setReceiver(rs.getString("Receiver"));
		l_entity.setCurrency(rs.getString("Currency"));
		l_entity.setAmount(rs.getDouble("Amount"));
		l_entity.setPaided(rs.getString("Paided"));
		l_entity.setReturn_code(rs.getString("ReturnStatus"));
		l_entity.setApply(rs.getString("Apply"));
		l_entity.setBalance(rs.getLong("Balance"));
		l_entity.setTotalamount(rs.getLong("TotalAmount"));

		return l_entity;
	}

}
